import _ from 'underscore';

export function Mock(config) {
  var _httpHandlers = {};
  var _realAdpter = () => {};
  var _axios;

  const getRequest = config => {
    var requset;
    var req = config.data;
    return req;
  };

  const adapter = (config) => {
    return new Promise(async (resolve, reject) => {
      console.log(JSON.parse(JSON.stringify(config)));
      const { url } = config;
      const callBack = _httpHandlers[url];
      const requset = getRequest(config);
      const response = callBack(requset);
      setTimeout(() => {
        resolve({
          status: 200,
          statusText: 'Ok',
          data: response,
          headers: config.headers,
          config
        });
      }, _.random(200, 1000))
    });
  };

  this.interceptHttp = (Axios) => {
    _axios = Axios;
    _realAdpter = Axios.defaults.adapter;
    _axios.defaults.adapter = adapter;
  };

  this.use = (path, callBack) => {
    _httpHandlers[path] = callBack;
  };
}

export default Mock;
